import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
// import { routes } from "./routes";
const modules: Record<
  string,
  {
    [key: string]: any;
  }
> = import.meta.glob('./modules/**/*.ts', { eager: true });

export let routes: RouteRecordRaw[] = [];
Object.keys(modules).forEach(key => {
  const mod = modules[key].default || {};
  const modList = Array.isArray(mod) ? [...mod] : [mod];
  routes.push(...modList);
});
console.log(routes);
const router = createRouter({
  history: createWebHistory(),
  routes: routes,
});
const whiteList = ['/login', '/register', '/registeredOk', '/user/authpass', '/forgetPassword'];
router.beforeEach((to, from, next) => {
  window.scrollTo(0, 0);
  if (whiteList.includes(to.path)) {
    next();
  } else {
    const token = localStorage.getItem('access_token');
    if (token === null || token === undefined || token === '') {
      next('login');
    } else {
      next();
    }
  }
});
export default router;
